import vm from '../main.js'
// 整数数字函数
function isInteger (value) {
  return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
}

// 必填
export const isRequired = (rule, value, callback) => {
  if (!value.toString()) {
    callback(new Error(vm.$t('不能为空')))
  } else {
    callback()
  }
}
// iVlanId
export const iVlanId = (rule, value, callback) => {
  if (!value) {
    callback(new Error(vm.$t('VLAN Id不能为空')))
  } else if (!isInteger(value)) {
    callback(new Error(vm.$t('VLAN Id必须为整数数字')))
  } else if (Number(value) > 4094 || Number(value) < 1) {
    callback(new Error((vm.$t('VLAN Id的范围为1到4094'))))
  } else {
    callback()
  }
}

// vlanTag
export const vlanTag = (rule, value, callback) => {
  if (!value) {
    callback(new Error(vm.$t('VLAN Tag不能为空')))
  } else if (!isInteger(value)) {
    callback(new Error(vm.$t('VLAN Tag必须为整数数字')))
  } else if (Number(value) > 4094 || Number(value) < 1) {
    callback(new Error((vm.$t('VLAN Tag的范围为1到4094'))))
  } else {
    callback()
  }
}

// iMtu
export const iMtu = (rule, value, callback) => {
  if (!value) {
    callback(new Error(vm.$t('MTU不能为空')))
  } else if (!isInteger(value)) {
    callback(new Error(vm.$t('MTU请输入整数')))
  } else if (Number(value) > 9216 || Number(value) < 68) {
    callback(new Error((vm.$t('MTU请输入68到9216之间的整数！'))))
  } else {
    callback()
  }
}

// 子接口-iMtu
export const iMtuSubInterface = (rule, value, callback) => {
  if (!value) {
    callback(new Error(vm.$t('MTU不能为空')))
  } else if (!isInteger(value)) {
    callback(new Error(vm.$t('MTU请输入整数')))
  } else if (Number(value) < 68) {
    callback(new Error((vm.$t('MTU请输入最小值为68的整数！'))))
  } else {
    callback()
  }
}

export default {
  isRequired: [{ required: true, validator: isRequired, trigger: ['blur', 'change'] }],
  iVlanId: [{ required: true, validator: iVlanId, trigger: 'blur' }],
  iMtu: [{ required: true, validator: iMtu, trigger: 'blur' }],
  iMtuSubInterface: [{ required: true, validator: iMtuSubInterface, trigger: 'blur' }],
  vlanTag: [{ required: true, validator: vlanTag, trigger: 'blur' }],
}